<template>
  <div>
    <el-dialog v-bind="$attrs" :visible="showAddEmp" title="新增员工" v-on="$listeners" @open="onOpen" @close="close">
      <el-form ref="formRef" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入姓名" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{width: '100%'}"
            placeholder="请选择入职时间"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select
            v-model="form.formOfEmployment"
            placeholder="请选择聘用形式"
            clearable
            :style="{width: '100%'}"
          >
            <el-option
              v-for="(item, index) in formOfEmploymentOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber" placeholder="请输入工号" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="部门 " prop="departmentName">
          <!-- 下拉显示树形结构失败，本身不支持 -->
          <!-- <el-select v-model="form.departmentName" placeholder="请选择部门" clearable :style="{width: '100%'}">
            <el-option
              v-for="(item, index) in departmentNameOptions"
              :key="index"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select> -->
          <!-- 通过悬浮和树形组件结合一起的方式 -->

          <el-popover
            v-model="showSelectDept"
            placement="bottom"
            width="480"
            trigger="click"
          >
            <el-tree
              :expand-on-click-node="false"
              :data="departmentNameOptions"
              :props="{label:'name'}"
              default-expand-all
              @node-click="onNodeClick"
            >
            </el-tree>
            <el-input
              slot="reference"
              v-model="form.departmentName"
              placeholder="请选择部门"
              clearable
              :style="{width:'100%'}"
            ></el-input>
          </el-popover>

        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{width: '100%'}"
            placeholder="请选择转正时间"
            clearable
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments } from '@/api/departments.js'
import Employees from '../../../api/constant/employees.js'
import { translateToTree } from '@/utils/index.js'
import { addEmployees } from '../employees.js'
export default {
  name: 'AddEmp',
  components: {

  },
  inheritAttrs: false,
  props: {
    showAddEmp: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        username: undefined,
        mobile: undefined,
        timeOfEntry: null,
        formOfEmployment: undefined,
        workNumber: undefined,
        departmentName: undefined,
        correctionTime: null
      },
      rules: {
        username: [],
        mobile: [],
        timeOfEntry: [],
        formOfEmployment: [],
        workNumber: [],
        departmentName: [],
        correctionTime: []
      },
      formOfEmploymentOptions: Employees.hireType,
      departmentNameOptions: [],
      showSelectDept: false// 数据弹框显示
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getDepartmentsFn()
  },
  mounted() {},
  methods: {
    onOpen() {},
    close() {
      this.$emit('update:showAddEmp', false)
      this.$refs['formRef'].resetFields()
    },
    handelConfirm() {
      // 表单验证（validate）
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        // 新增员工添加数据 调接口
        await addEmployees(this.form)
        // 提示成功
        this.$message.success('操作成功')
        // 刷新列表
        this.$emit('success')
        // 关闭弹层
        this.close()
      })
    },
    // 获取树形数据
    async getDepartmentsFn() {
      const res = await getDepartments()
      const list = translateToTree(res.depts, '')
      this.departmentNameOptions = list
    },
    // 关闭弹层和保存数据
    onNodeClick(data) {
      this.showSelectDept = false
      this.form.departmentName = data.name
    }
  }
}

</script>
<style lang="scss" scoped>
::v-deep .el-tree-node__expand-icon{
  visibility: hidden;
}
</style>
